from django.http import JsonResponse
import json

from salary_flow.models import Inner_branchoffice


# 分配器

def dispatcher(request):
    # 将请求同意放在request的params中
    print(request.body)
    if request.method == 'GET':
        request.params = request.GET
    elif request.method in ['POST', 'PUT', 'DELETE']:
        request.params = json.loads(request.body)

    # 根据不同的action分派给不同的函数进行处理
    action = request.params['action']
    if action == 'list_inner_branchoffice':
        return list_inner_branchoffice(request)
    elif action == 'add_inner_branchoffice':
        return add_inner_branchoffice(request)
    elif action == 'modify_inner_branchoffice':
        return modify_inner_branchoffice(request)
    elif action == 'del_inner_branchoffice':
        return del_inner_branchoffice(request)

    else:
        return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'}, json_dumps_params={'ensure_ascii': False})


def list_inner_branchoffice(request):
    # 返回一个 QuerySet 对象 ，包含所有的表记录
    qs = Inner_branchoffice.objects.values()

    # 将 QuerySet 对象 转化为 list 类型
    # 否则不能 被 转化为 JSON 字符串
    retlist = list(qs)

    return JsonResponse({'ret': 0, 'retlist': retlist}, json_dumps_params={'ensure_ascii': False})


def add_inner_branchoffice(request):
    print(request)
    info = request.params['data']

    # 从请求消息中 获取要添加部门的信息
    # 并且插入到数据库中
    # 返回值 就是对应插入记录的对象
    record = Inner_branchoffice.objects.create(NBname=info['name'],
                                               NBstate=info['state'],
                                               NBstatetext=info['statetext'],
                                               )

    return JsonResponse({'ret': 0, 'id': record.id}, json_dumps_params={'ensure_ascii': False})

def modify_inner_branchoffice(request):
    # 从请求消息中 获取修改部门的信息
    # 找到该部门，并且进行修改操作

    Inner_branchoffice_id = request.params['id']
    newdata = request.params['newdata']

    try:
        # 根据 id 从数据库中找到相应的部门记录
        inner_branchoffice = Inner_branchoffice.objects.get(id=Inner_branchoffice_id)
    except Inner_branchoffice.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'id 为`{Inner_branchoffice_id}`的部门不存在'
        }, json_dumps_params={'ensure_ascii':False})

    if 'name' in newdata:
        inner_branchoffice.NBname = newdata['name']
    if 'state' in newdata:
        inner_branchoffice.NBstate = newdata['state']
    if 'statetext' in newdata:
        inner_branchoffice.NBstatetext = newdata['statetext']


    # 注意，一定要执行save才能将修改信息保存到数据库
    inner_branchoffice.save()

    return JsonResponse({'ret': 0}, json_dumps_params={'ensure_ascii':False})

def del_inner_branchoffice(request):
    Inner_branchoffice_id = request.params['id']

    try:
        # 根据 id 从数据库中找到相应的部门记录
        inner_branchoffice = Inner_branchoffice.objects.get(id=Inner_branchoffice_id)
    except Inner_branchoffice.DoesNotExist:
        return JsonResponse({
            'ret': 1,
            'msg': f'id 为`{Inner_branchoffice_id}`的部门不存在'
        }, json_dumps_params={'ensure_ascii':False})

    # delete 方法就将该记录从数据库中删除了
    inner_branchoffice.delete()

    return JsonResponse({'ret': 0}, json_dumps_params={'ensure_ascii':False})
